
{extend name="public:base" /}
{block name="css"}<link rel="stylesheet" href="/static/admin/tree/css/treeTable.css">{/block}
{block name="body"}


<body class="layui-hide">

<div class="page-wrapper">

    <div class="demo-side">
        <table id="LAY-user-auth-rule"></table>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="tbBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <!--a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a-->
	{{#  if(d.id == '1' | d.id == '2' | d.id == '9' | d.id == '10' ){ }}<a class="layui-btn layui-btn-disabled layui-btn-xs">删除</a>
    {{#  } else { }}
		{if condition="checkRuleButton('authrule/delete')"}<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
		{else /}<a class="layui-btn layui-btn-danger layui-btn-xs layui-btn-disabled">删除</a>{/if}
	{{#  } }}
</script>

{/block}

{block name="js"}
<script>
    layui.config({
        base: '/static/admin/modules/'
    }).use(['layer', 'util', 'treeTable'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var util = layui.util;
        var treeTable = layui.treeTable;
        $('body').removeClass('layui-hide');

        // 渲染表格
        var insTb = treeTable.render({
            elem: '#LAY-user-auth-rule',
            url: "{:url('AuthRule/index')}",
            toolbar: 'default',
            //height: 'full-200',
            tree: {
                iconIndex: 2,
                isPidData: true,
                idName: 'id',
                pidName: 'pid'
            },
            defaultToolbar: ['filter', 'print', 'exports'],
            cols: [
                [
                    {type: 'numbers'},
                    {type: 'checkbox'},
                    {field: 'title', title: '菜单名称', minWidth: 165},
					{field: 'url', title: '菜单地址', rowspan: 2},
                    {
                        title: '菜单图标', align: 'center', 
                        templet: '<p><i class="layui-icon {{d.icon}}"></i></p>'
                    },
                    {field: 'authority', title: '权限标识'},
					{field: 'isMenu', title: '类型', templet: type, align: 'center', width: 60},
                    //{title: '类型', templet: '<p>{{d.isMenu ? "菜单" : "按钮"}}</p>', align: 'center', width: 60},
					{field: 'sort', title: '排序', align: 'center', width: 60},
                    {field: 'ctime',title: '创建时间'},
                    {align: 'center', toolbar: '#tbBar', title: '操作', width: 120}
                ]
            ],
            style: 'margin-top:0;'
        });
		
		//自定义“状态”列
		function type(data) {
			var isMenu = data.isMenu;
			var btns = "";
			if (isMenu == -1) {
				return "目录";
			}
			if (isMenu == 1) {
			btns += '<span class="layui-btn layui-btn-sm">菜单</span>';
				//return "菜单";
			}
			if (isMenu == 0) {
			btns += '<span class="layui-btn layui-btn-normal layui-btn-sm">按钮</span>';
				//return "按钮";
			}
			return btns;
		}


        // 工具列点击事件
        treeTable.on('tool(LAY-user-auth-rule)', function (obj) {
            var event = obj.event;
			var data = obj.data;
            if (event === 'del') {
			
				layer.prompt({
					formType: 1
					,title: '敏感操作，请验证口令'
				  }, function(value, index){
					layer.close(index);
					layer.confirm('确定删除此权限？', function(index){
					 $.ajax({
							type:'post',
							url:"{:url('AuthRule/delete')}",
							data:{id:data.id},
							dataType:'json',
							success:function(data){
								if(data.code == 0){
									layer.msg(data.msg,{
										icon:6,
										time:2000
									},function(){
										location.reload();
									});
								} else {
									layer.open({
										title:'删除失败',
										content:data.msg,
										icon:5,
										adim:6
									})
								}
							}
						});
					  layer.close(index);
					});
				  });
			
            } else if (event === 'edit') {

				layer.open({
					type: 2
					,title: '编辑权限'
					,content: "{:url('AuthRule/edit')}" +'?id='+ data.id
					,area: ['420px', '550px']
					,btn: ['确定', '取消']
					,yes: function(index, layero){
					  var iframeWindow = window['layui-layer-iframe'+ index]
					  ,submitID = 'LAY-user-rule-submit'
					  ,submit = layero.find('iframe').contents().find('#'+ submitID);
					  
					  //监听提交
					  iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
						var field = data.field; //获取提交的字段
						
						//提交 Ajax 成功后，静态更新表格中的数据
						$.ajax({
							type:"post",
							url:"{:url('AuthRule/edit')}",
							data:{"id":field.id,"pid":field.pid,"title":field.title,"name":field.name,"icon":field.icon,"sort":field.sort,"ishidden":field.ishidden},
							daType:"json",
							success:function (res){
								if (res.code == 0) {
									layer.msg(res.msg,{
										icon:6,
										time:2000
									}, function(){
										location.reload();
									});
								} else {
									layer.open({
										tiele:'修改失败',
										content:res.msg,
										icon:5,
										anim:6
									});
								}
							}
						});
						
						//treeTable.reload('LAY-user-auth-rule'); //数据刷新
						layer.close(index); //关闭弹层
					  }); 
					  
					  submit.trigger('click');
					}
					,success: function(layero, index){
					
					}
				  });
            }
        });

        // 头部工具栏点击事件
        treeTable.on('toolbar(LAY-user-auth-rule)', function (obj) {
            switch (obj.event) {
                case 'add':
                    layer.msg('添加');
					layer.open({
					  type: 2
					  ,title: '添加权限'
					  ,content: 'add.html'
					  ,area: ['420px', '550px']
					  ,btn: ['确定', '取消']
					  ,yes: function(index, layero){
						var iframeWindow = window['layui-layer-iframe'+ index]
						,submitID = 'LAY-user-rule-submit'
						,submit = layero.find('iframe').contents().find('#'+ submitID);

						//监听提交
						iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
						  var field = data.field; //获取提交的字段
						  
						  //提交 Ajax 成功后，静态更新表格中的数据
						  $.ajax({
							type:"post",
							url:"{:url('AuthRule/add')}",
							data:{"pid":field.pid,"title":field.title,"name":field.name,"icon":field.icon,"sort":field.sort,"ishidden":field.ishidden},
							daType:"json",
							success:function (data){
								if (data.code == 0) {
									layer.msg(data.msg,{
										icon:6,
										time:2000
									}, function(){
										location.reload();
									});
								} else {
									layer.open({
										title:'添加失败',
										content:data.msg,
										icon:5,
										anim:6
									});
								}
							}
						}); 
						  treeTable.reload('LAY-user-auth-rule'); //数据刷新
						  layer.close(index); //关闭弹层
						});  
						
						submit.trigger('click');
					  }
					}); 
				
                    break;
                case 'delete':
                    layer.msg('删除');
                    break;
                case 'update':
                    layer.msg('编辑');
                    break;
                case 'LAYTABLE_TIPS':
                    layer.msg('提示');
                    break;
            }
        });

        // 全部展开
        $('#btnExpandAll').click(function () {
            insTb.expandAll();
        });

        // 全部折叠
        $('#btnFoldAll').click(function () {
            insTb.foldAll();
        });

        // 展开指定
        $('#btnExpand').click(function () {
            insTb.expand(2);
        });

        // 折叠指定
        $('#btnFold').click(function () {
            insTb.fold(2);
        });

        // 设置选中
        $('#btnChecked').click(function () {
            insTb.expand(4);
            insTb.setChecked([4]);
        });

        // 搜索
        $('#btnSearch').click(function () {
            var keywords = $('#edtSearch').val();
            if (keywords) {
                insTb.filterData(keywords);
            } else {
                insTb.clearFilter();
            }
        });

        // 清除搜索
        $('#btnClearSearch').click(function () {
            insTb.clearFilter();
        });

        // 重载
        $('#btnReload').click(function () {
            insTb.reload();
        });
        $('#btnRefresh').click(function () {
            insTb.refresh();
        });

        // 获取选中
        $('#btnGetChecked').click(function () {
            layer.alert('<pre>' + JSON.stringify(insTb.checkStatus().map(function (d) {
                return {
                    authorityName: d.authorityName,
                    authorityId: d.authorityId,
                    LAY_INDETERMINATE: d.LAY_INDETERMINATE
                };
            }), null, 3) + '</pre>');
        });

        // 演示侧边栏
        $('#btnToggleSide').click(function () {
            $('.demo-side').toggleClass('show');
        });

    });
</script>
{/block}